package me.obstsalatt.guildera2.core;

/**
 * Created with IntelliJ IDEA.
 * User: Robin
 * Date: 14.02.14, 19:45
 * Project: GuildEra2
 * Package: me.obstsalatt.guildera2.core
 * Descriprion:
 *  Just the constants for rights and a method to recreate them out of an integer!
 */
public abstract class GuildRights {
    public final static int MAX_RIGHT_ATTRIBUTES = 6;
    public final static int DECLARE_ENEMY = 1;
    public final static int ACCESS_BANK = 2;
    public final static int ALLOW_KICK = 4;
    public final static int ALLOW_INVITE = 8;
    public final static int CAN_PROMOTE = 16;
    public final static int CREATE_RANKS = 32;

    public static int[] fromValue(int i) {
        int[] rights = new int[0];
        if(i > 31) {
            rights = extendArray(rights, CREATE_RANKS);
            i -= 32;
        }
        if(i > 15) {
            rights = extendArray(rights, CAN_PROMOTE);
            i -= 16;
        }
        if(i > 7) {
            rights = extendArray(rights, ALLOW_INVITE);
            i -= 8;
        }
        if(i > 3) {
            rights = extendArray(rights, ALLOW_KICK);
            i -= 4;
        }
        if(i > 1) {
            rights = extendArray(rights, ACCESS_BANK);
            i -= 2;
        }
        if(i == 1) {
            rights = extendArray(rights, DECLARE_ENEMY);
        }
        return rights;
    }

    private static int[] extendArray(int[] arr, int i) {
        int[] cache = new int[arr.length + 1];
        for(int k = 0; k < arr.length; k++) {
            cache[k] = arr[k];
        }
        cache[arr.length] = i;
        return cache;
    }
}
